using System;
using System.Collections.Generic;
using ColorBox4Sitecore.Helpers;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;

namespace ColorBox4Sitecore.Colorbox
{
    public class ColorboxSitecore : Colorbox
    {
        public ColorboxSitecore(Item source)
        {
            if (source == null || source == Sitecore.Context.Item)
            {
                Log.Error("Colorbox - Constructor - The datasource cannot be null or empty", this);
                throw new NullReferenceException("Colorbox - Constructor - The datasource cannot be null or empty");
            }
            else
            {
                ColorboxDefaultValuesSitecore defaultValues = new ColorboxDefaultValuesSitecore(source);

                SelectorCssClass = source.GetStringValueFromField("Selector Css Class", defaultValues.SelectorCssClass);

                ScalePhotos = source.GetBoolValueFromField("Scale Photos", defaultValues.ScalePhotos);
                Scrolling = source.GetBoolValueFromField("Scrolling", defaultValues.Scrolling);
                Transition = source.GetColorboxTransitionValueFromField("Transition", defaultValues.Transition);
                Speed = source.GetIntValueFromField("Speed", defaultValues.Speed);


                Href = source.GetUrlFromGeneralLink("Href", defaultValues.Href);
                Title = source.GetStringValueFromField("Title", defaultValues.Title);
                Rel = source.GetStringValueFromField("Rel", defaultValues.Rel);
                Current = source.GetStringValueFromField("Current", defaultValues.Current);
                Previous = source.GetStringValueFromField("Previous", defaultValues.Previous);
                Next = source.GetStringValueFromField("Next", defaultValues.Next);
                Close = source.GetStringValueFromField("Close", defaultValues.Close);
                XhrError = source.GetStringValueFromField("XhrError", defaultValues.XhrError);
                ImgError = source.GetStringValueFromField("ImgError", defaultValues.ImgError);

                Opacity = source.GetIntValueFromField("Opacity", defaultValues.Opacity);
                Open = source.GetBoolValueFromField("Open", defaultValues.Open);
                ReturnFocus = source.GetBoolValueFromField("Return Focus", defaultValues.ReturnFocus);
                FastIframe = source.GetBoolValueFromField("Fast Iframe", defaultValues.FastIframe);
                Preloading = source.GetBoolValueFromField("Preloading", defaultValues.Preloading);
                OverlayClose = source.GetBoolValueFromField("Overlay Close", defaultValues.OverlayClose);
                EscKey = source.GetBoolValueFromField("Esc Key", defaultValues.EscKey);
                ArrowKey = source.GetBoolValueFromField("Arrow Key", defaultValues.ArrowKey);
                Loop = source.GetBoolValueFromField("Loop", defaultValues.Loop);
                Data = source.GetBoolValueFromField("Data", defaultValues.Data);

                Iframe = source.GetBoolValueFromField("Iframe", defaultValues.Iframe);
                Inline = source.GetBoolValueFromField("Inline", defaultValues.Inline);
                Html = source.GetStringValueFromField("Html", defaultValues.Html);
                Photo = source.GetBoolValueFromField("Photo", defaultValues.Photo);

                Width = source.GetIntValueFromField("Width", defaultValues.Width);
                WidthUnit = source.GetColorboxUnitValueFromField("Width Unit", defaultValues.WidthUnit);
                Height = source.GetIntValueFromField("Height", defaultValues.Height);
                HeightUnit = source.GetColorboxUnitValueFromField("Height Unit", defaultValues.HeightUnit);
                InnerWidth = source.GetIntValueFromField("Inner Width", defaultValues.InnerWidth);
                InnerWidthUnit = source.GetColorboxUnitValueFromField("Inner Width Unit", defaultValues.InnerWidthUnit);
                InnerHeight = source.GetIntValueFromField("Inner Height", defaultValues.InnerHeight);
                InnerHeightUnit = source.GetColorboxUnitValueFromField("Inner Height Unit", defaultValues.InnerHeightUnit);
                InitialWidth = source.GetIntValueFromField("Initial Width", defaultValues.InitialWidth);
                InitialWidthUnit = source.GetColorboxUnitValueFromField("Initial Width Unit", defaultValues.InitialWidthUnit);
                InitialHeight = source.GetIntValueFromField("Initial Height", defaultValues.InitialHeight);
                InitialHeightUnit = source.GetColorboxUnitValueFromField("Initial Height Unit", defaultValues.InitialHeightUnit);
                MaxWidth = source.GetIntValueFromField("Max Width", defaultValues.MaxWidth);
                MaxWidthUnit = source.GetColorboxUnitValueFromField("Max Width Unit", defaultValues.MaxWidthUnit);
                MaxHeight = source.GetIntValueFromField("Max Height", defaultValues.MaxHeight);
                MaxHeightUnit = source.GetColorboxUnitValueFromField("Max Height Unit", defaultValues.MaxHeightUnit);

                SlideshowEnable = source.GetBoolValueFromField("Slideshow Enable", defaultValues.SlideshowEnable);
                SlideshowSpeed = source.GetIntValueFromField("Slideshow Speed", defaultValues.SlideshowSpeed);
                SlideshowAuto = source.GetBoolValueFromField("Slideshow Auto", defaultValues.SlideshowAuto);
                SlideshowStartText = source.GetStringValueFromField("Slideshow Start Text", defaultValues.SlideshowStartText);
                SlideshowEndText = source.GetStringValueFromField("Slideshow End Text", defaultValues.SlideshowEndText);

                Fixed = source.GetBoolValueFromField("Fixed", defaultValues.Fixed);
                Top = source.GetIntValueFromField("Top", defaultValues.Top);
                TopUnit = source.GetColorboxUnitValueFromField("Top Unit", defaultValues.TopUnit);
                Bottom = source.GetIntValueFromField("Bottom", defaultValues.Bottom);
                BottomUnit = source.GetColorboxUnitValueFromField("Bottom Unit", defaultValues.BottomUnit);
                Left = source.GetIntValueFromField("Left", defaultValues.Left);
                LeftUnit = source.GetColorboxUnitValueFromField("Left Unit", defaultValues.LeftUnit);
                Right = source.GetIntValueFromField("Right", defaultValues.Right);
                RightUnit = source.GetColorboxUnitValueFromField("Right Unit", defaultValues.RightUnit);

                var pictures = source.GetItemsFromMultilistField("Slideshow Pictures", defaultValues.SlideshowPictures);
                SlideshowPictures = new List<KeyValuePair<string, string>>();

                int? scaleHeight = ColorboxHelper.GetSize(MaxHeight, Height, InnerHeight);
                int? scaleWidth = ColorboxHelper.GetSize(MaxWidth, Width, InnerWidth);

                foreach (var pic in pictures)
                {
                    SlideshowPictures.Add(new KeyValuePair<string, string>(ColorboxHelper.GetPictureUrl(pic, ScalePhotos, scaleHeight, scaleWidth), ((MediaItem)pic).Alt));
                }

                AdditionalProperties = source.GetStringValueFromField("Additional Properties", defaultValues.AdditionalProperties);
                
            }
        }

        

        
    }
}